var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,
    $ = layui.jquery,
    laydate = layui.laydate;

layui.use(['tableEdit',"excel"], function () {
    let tableEdit = layui.tableEdit;
    let layer = layui.layer,
        excel = layui.excel;


    lay('.starttime').each(function(){
        laydate.render({
            elem: this
        });
    });
    function getAllJson(jsons) {
        let tmpitem=[]
        for (let item of jsons) {
            let tmparr={}
            tmparr["value"]=item["code"];
            tmparr["name"]=item["name"];
            if(item["users"]){
                for(let it of item["users"]){
                    item["list"].push(it)
                }
            }
            if(item["list"] && item["list"].length){
                tmparr["children"]=   getAllJson(item["list"]);
                tmpitem.push(tmparr)
            }else{
                tmpitem.push(tmparr)
            }

        }
        return tmpitem
    }
    function getxmselect(el,name, data) {
        return xmSelect.render({
            el: el,
            name: name,
            tree: {
                show: true,
                showLine: true,
                showFolderIcon:true,
                indent: 20,
                expandedKeys:false
            },
            height: 'auto',
            data: data,
            on: function ({ arr, change, isAdd }) {
                return this
            }
        })
    }

    let operationList=[];
    let salesmanList=[];
    let deptSalesmanList = [];
    let deptOperationList=[];

    (function () {
        //获取当前用户的部门及人员结构树
        let treeList = getCurrentDepartmentUser();

        if(treeList && treeList.list){
            let userTree = getAllJson(treeList.list);
            operationList=getxmselect("#operationCode","operationCode", userTree);
            salesmanList=getxmselect("#salesmanCode","salesmanCode", userTree);
        }else {
            operationList=getxmselect("#operationCode","operationCode", [{name: user.name, value: user.code}]);
            salesmanList=getxmselect("#salesmanCode","salesmanCode", [{name: user.name, value: user.code}]);
        }

        //获取当前用户所属部门树()
        let currentDeptTreeList = getAllUserDepartmentTree();
        if (currentDeptTreeList && currentDeptTreeList.list) {
            let deptTree = getAllJson(currentDeptTreeList.list);
            deptSalesmanList = getxmselect("#deptSalesmanCode", "deptSalesmanCode", deptTree);
            deptOperationList = getxmselect("#deptOperationCode", "deptOperationCode", deptTree);
        } else {
            deptOperationList = getxmselect("#deptOperationCode", "deptOperationCode", [{name: user.name, value: user.code}]);
            deptSalesmanList = getxmselect("#deptSalesmanCode", "deptSalesmanCode", [{name: user.name, value: user.code}]);
        }
    })()
    layer.ready(function () {

        //获取所有公司信息
        $.ajax({
            url:"receivePayment/getCompanyInfo",
            success:function(data){
                if(data.status == 200){
                    for(let i in data.data){
                        //公司名称
                        $('#signingCompanyCode').append(new Option(data.data[i].name, data.data[i].code));
                    }
                    form.render('select');
                }
            }
        });

        //获取币种
        $.ajax({
            url:"receivePayment/getCurrencyInfo",
            success:function(data){
                if(data.status == 200){
                    for(let i in data.data){
                        $('#currencyCode').append(new Option(data.data[i].name, data.data[i].code));
                    }
                    form.render('select');
                }
            }
        });



        //获取所有银行信息
        $.ajax({
            url:"receivePayment/getCompanyInfo",
            success:function(data){
                if(data.status == 200){

                }
            }
        });
        //获取所有客户信息
        $.ajax({
            url:"otherOutStock/add/getCustomerAllInfo",
            success:function(data){
                if(data.status == 200){
                    customerInfo = data.data;
                    for(let i in data.data){
                        //客户名称
                        $('#customerCode').append(new Option(data.data[i].name, data.data[i].code));
                    }
                    form.render('select');
                }
            }
        });

    });
    form.render();//表单刷新
    let initData = [];
    table.render({
        elem: '#collectionSelect'
        ,data:initData
        ,toolbar: '#collectionToolbar'
        ,totalRow: true
        ,cols: [[
            {field: 'numbers',width: 50,type:'numbers',title:'序号', totalRowText: '合计'}
            , {field: 'timeStr', title: '收款日期', sort: true, width: 120, align: 'center'}
            , {field: 'billNo', title: '收款编号', sort: true, width: 200, align: 'center'}
            , {field: 'customerName', title: '客户名称', sort: true, width: 120, align: 'center'}
            , {field: 'collectionAmount', title: '收汇金额', sort: true, width: 120, align: 'center', totalRow: true}
            , {field: 'entryAmount', title: '入账金额', sort: true, width: 120, align: 'center', totalRow: true}
            , {field:'bankDeduction', title:'银行扣费',sort:true,width: 120, align: 'center', totalRow: true}
            , {field: 'actualCollectionAmount', title: '实际收汇金额', sort: true, width: 200, align: 'center', totalRow: true}
            , {field:'currencyEnglishName', title:'币种',sort:true,width: 120, align: 'center'}
            , {field: 'claimed', title: '实际合同认领金额', sort: true, width: 200, align: 'center', totalRow: true}
            , {field: 'claimedUsd', title: '实际合同认领金额USD', sort: true, width: 200, align: 'center', totalRow: true}
            , {field:'priceTermsName', title:'价格术语',sort:true,width: 120, align: 'center'}
            , {field:'claimSalesman', title:'认领业务员名称',sort:true,width: 200, align: 'center'}
            , {field: 'claimCustomer', title: '认领客户名称', sort: true, width: 200, align: 'center'}
            , {field: 'contractNo', title: '认领合同号', sort: true, width: 120, align: 'center'}
            , {field: 'customerSource', title: '客户来源', sort: true, width: 200, align: 'center'}
            , {field: 'attributesOfCustomers', title: '客户属性' ,width: 200}
            , {field: 'claimDateStr', title: '认领日期', sort: true, width: 120, align: 'center'}
            , {field: 'abbreviation', title: '现在客户简称', sort: true, width: 200, align: 'center'}
            , {field: 'minClaimDateStr', title: '第一次认领日期', sort: true, width: 200, align: 'center'}


            // , {field: 'customerCode', title: '客户编码', sort: true, width: 120, align: 'center'}
            // , {field: 'operationNo', title: '操作员编码', sort: true, width: 120, align: 'center'}
            // , {field: 'operator', title: '操作员名称', sort: true, width: 120, align: 'center'}
            // , {field: 'bankName', title: '收款银行', sort: true, width: 120, align: 'center'}
            // , {field: 'accountNo', title: '收款账号', sort: true, width: 120, align: 'center'}
            // , {field: 'bankDeductionCapital', title: '国内银行扣费', sort: true, width: 200, align: 'center', totalRow: true}
            // , {field: 'claimedRmb', title: '实际合同认领金额RMB', sort: true, width: 200, align: 'center', totalRow: true}
            // , {field: 'claimedUsd', title: '实际合同认领金额USD', sort: true, width: 200, align: 'center', totalRow: true}
            // , {field:'foreignBankDeductions', title:'具体国外银行扣费',sort:true,width: 200, align: 'center', totalRow: true}
            // , {field:'domesticBankCharge', title:'具体国内银行扣费',sort:true,width: 200, align: 'center', totalRow: true}
            // , {field:'contractSalesman', title:'销售合同建单业务员',sort:true,width: 200, align: 'center'}
            // , {field:'collectionAmountRmb', title:'收汇',sort:true,width: 120, align: 'center', totalRow: true}
            // , {field: 'amountClaimedRmb', title: '入账', sort: true, width: 120, align: 'center', totalRow: true}
            // , {field: 'nowCustomerName', title: '现在客户名称', sort: true, width: 200, align: 'center'}
            // , {field: 'salesmanCode', title: '业务员编码', sort: true, width: 120, align: 'center'}
            // , {field: 'salesman', title: '业务员名称', sort: true, width: 120, align: 'center'}
        ]]
        ,id: 'maininformationtableid'
        ,height: $(document).height() - $('#collectionSelect').offset().top - 75,
        //开启分页
        page: true,
        limit:300,
        limits: [300,600,900],
    });


//搜索提交
    form.on('submit(submitform)', function(data){
        if (!isEmpty(salesmanList.getValue('nameStr'))){
            data.field.salesmanCode=salesmanList.getValue('nameStr');//获取name字符串
        }
        if (!isEmpty(deptSalesmanList.getValue('nameStr'))){
            data.field.deptSalesmanCode=deptSalesmanList.getValue('nameStr');//获取选中的name字符串
        }

        table.reload('maininformationtableid', {
            page: {
                limit:300,
                curr: 1
            },
            url:'receivePayment/collectionStatistics1ListByPage',
            where:  data.field
        }, 'data');
        return false;
    });

    form.on('submit(collectionExcel)', function (data) {
        if (!isEmpty(salesmanList.getValue('nameStr'))){
            data.field.salesmanCode=salesmanList.getValue('nameStr');//获取name字符串
        }
        if (!isEmpty(deptSalesmanList.getValue('nameStr'))){
            data.field.deptSalesmanCode=deptSalesmanList.getValue('nameStr');//获取选中的name字符串
        }
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: data.field,
            url:'receivePayment/collectionStatistics1Export',
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 120,
                        'AJ': 120
                    }, 120);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '收款金额明细统计列表.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });


    document.getElementById('clear').onclick = function(){
        operationList.setValue([ ]);
        salesmanList.setValue([ ]);
        deptSalesmanList.setValue([ ]);
        deptOperationList.setValue([ ]);
    };
    function isEmpty(obj){
        if(typeof obj == "undefined" || obj == null || obj == "" || obj == 0){
            return true;
        }else{
            return false;
        }
    }

});
